﻿using JXT.PrimaryKey.Batman.Container.Unity;

namespace JXT.PrimaryKey.Batman.Container
{
    /// <summary>
    /// 容器管理器
    /// </summary>
    public static class ContainerManager {
        /// <summary>
        /// 获取默认容器，调用等同于GetContainer("default")
        /// </summary>
        /// <returns>默认的容器对象</returns>
        public static IContainer GetContainer() {
            return GetContainer("default");
        }

        /// <summary>
        /// 获取指定的容器
        /// </summary>
        /// <param name="name">容器名</param>
        /// <returns>取得的容器对象</returns>
        public static IContainer GetContainer(string name) {
            return new UnityContainer(name);
        }

        private static readonly IContainer _repositoryContainer = new UnityContainer("Repository");
        private static readonly IContainer _serviceContainer = new UnityContainer("Service");
        private static readonly IContainer _defaultContainer = new UnityContainer("Default");
        private static readonly IContainer _validationContainer = new UnityContainer("Validation");
        private static readonly object _lockObject = new object();

        /// <summary>
        /// 存储对象容器
        /// </summary>
        public static IContainer Repository {
            get {
                //if (_repositoryContainer == null) {
                //    lock (_lockObject) {
                //        if (_repositoryContainer == null) {
                //            _repositoryContainer = new UnityContainer("Repository");
                //        }
                //    }
                //}
                return _repositoryContainer;
            }
        }

        /// <summary>
        /// 业务对象容器
        /// </summary>
        public static IContainer Service {
            get {
                return _serviceContainer;
            }
        }

        public static IContainer Validation
        {
            get {
                //if (_validationContainer == null)
                //{
                //    lock (_lockObject)
                //    {
                //        if (_validationContainer == null)
                //        {
                //            _validationContainer = new UnityContainer("Validation");
                //        }
                //    }
                //}
                return _validationContainer;
            }
        }

        /// <summary>
        ///  其他对象容器
        /// </summary>
        public static IContainer Default
        {
            get
            {
                //if (_defaultContainer == null)
                //{
                //    lock (_lockObject)
                //    {
                //        if (_defaultContainer == null)
                //        {
                //            _defaultContainer = GetContainer("Default");
                //        }
                //    }
                //}
                return _defaultContainer;
            }
        }
    }
}
